package com.mytest;

import com.mytest.bean.ConsumerComponent;
import com.mytest.utils.CountTime;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.*;

public class AnnotationConsumerMain {
    public static void main(String[] args) throws  Exception {
        System.out.println("-------------");
        AnnotationConfigApplicationContext   context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        ConsumerComponent  service = context.getBean(ConsumerComponent.class);
        /*while(true){
            System.in.read();
            String  hello = service.sayHelloA("world");
            System.out.println("result:"+hello);
        }*/
        //初始化延迟5秒执行，本次执行完毕后延迟100ms开始下次执行
        String name = "world";
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(6);
        /*executorService.scheduleAtFixedRate(()->{
            service.sayHelloA(name);
            service.sayHelloB(name);
            service.sayHelloC(name);
        },5,60,TimeUnit.MILLISECONDS);*/
        executorService.scheduleAtFixedRate(() -> {
            service.sayHelloA(name);
        }, 5, 60, TimeUnit.MILLISECONDS);

        executorService.scheduleAtFixedRate(() -> {
            service.sayHelloB(name);
        }, 5, 60, TimeUnit.MILLISECONDS);

        executorService.scheduleAtFixedRate(() -> {
            service.sayHelloC(name);
        }, 5, 60, TimeUnit.MILLISECONDS);

        /**
         * 创建定时任务
         * 每隔5s打印一次最近1分钟内每个方法的TP90、TP99的耗时情况
         */
         Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new CountTime(), 30, 5, TimeUnit.SECONDS);
    }



    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.mytest.bean")
    @EnableDubbo
    static  class  ConsumerConfiguration{

    }
}
